package com.ideazd.api.util;

import java.util.List;

import com.ideazd.api.model.KeyWord;

public class KeyWordReplace {
	
	private static String keyWords = "";
	
	 //从数据库获取数据源
	public static void  createDataSource(){
		List<KeyWord> wordList = KeyWord.dao.find("SELECT * FROM keyword WHERE ENABLE='0' ORDER BY LENGTH(word)  DESC");
		StringBuffer sb = new StringBuffer();
		for(KeyWord k:wordList){
			sb.append(k.getStr("word")).append(",");
		}
		keyWords = sb.toString();
		
	}
	
	public static String replaceKeyWord(String content){
		
		String [] keyWordArray = keyWords.split(",");
		
		for(String keyWord:keyWordArray){
			
			if(content.indexOf(keyWord) > -1){
				String startWord = "";
				
				for(int i = 0; i <keyWord.length(); i++){
					startWord +="*";
				}
				content = content.replaceAll(keyWord, startWord);
			}
		}
		
		return content;
	}
	
	public static boolean isIllegal(String content){
			
		String [] keyWordArray = keyWords.split(",");
		
		for(String keyWord:keyWordArray){
			
			if(content.indexOf(keyWord) > -1){
				return true;
			}
		}
		
		return false;
	}
	
	

}
